// Copyright (c) 2009 Paul Kohler
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
// and associated documentation files (the "Software"), to deal in the Software without restriction, 
// including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do 
// so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in all copies or substantial 
// portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// 
using System;
using System.IO;
using FileSystemDatabase.Exceptions;
using FileSystemDatabase.Tests.Models;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;

namespace FileSystemDatabase.Tests
{
	[TestFixture]
	public class XmlDatabase_save_tests
	{
		#region Setup/Teardown

		[SetUp]
		public void TestSetUp()
		{
			_db = new XmlDatabase(_path);
			_db.WriteIndented = true;

			string[] files = Directory.GetFiles(_path, "*.*", SearchOption.TopDirectoryOnly);
			foreach (string file in files)
			{
				if (File.Exists(file))
				{
					File.Delete(file);
				}
			}
		}

		#endregion

		[TestFixtureSetUp]
		public void TestFixtureSetUp()
		{
			_path = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"..\..\XmlDataOut"));
		}

		private string _path;
		private XmlDatabase _db;

		[Test]
		[ExpectedException(typeof(FileSystemDatabaseWriteException))]
		public void Can_NOT_save_a_Person_to_a_bad_dir()
		{
			_db = new XmlDatabase(_path + "blablabla");
			Person person = new Person { PersonId = "2" };
			_db.Write(person, person.PersonId);
		}

		[Test]
		public void Can_save_a_Person()
		{
			Person person = new Person { PersonId = "1", Name = "Bob", DOB = new DateTime(1968, 3, 7) };
			string filename = _db.Write(person, person.PersonId);

			// check basics
			Assert.That(filename, Text.EndsWith(@"\Person-1.xml"));
			Assert.IsTrue(File.Exists(filename), "Expect file: " + filename);

			// check data
			string fileData = File.ReadAllText(filename);
			Assert.That(fileData, Text.Contains("<PersonId>1</PersonId>"));
			Assert.That(fileData, Text.Contains("<Name>Bob</Name>"));
			Assert.That(fileData, Text.Contains("<DOB>1968-03-07"));

			/* sample...
				<?xml version="1.0" encoding="utf-8"?>
				<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
				  <PersonId>1</PersonId>
				  <Name>Bob</Name>
				  <DOB>1968-03-07T00:00:00</DOB>
				</Person>
			 */
		}

		[Test]
		public void Can_save_a_Pet()
		{
			Pet dog = new Pet { ID = _db.CreateID(), Name = "Charlie" };
			string filename = _db.Write(dog, dog.ID);

			// check basics
			Assert.That(filename, Text.Contains(@"\Pet-"));
			Assert.IsTrue(File.Exists(filename), "Expect file: " + filename);

			// check data
			string fileData = File.ReadAllText(filename);
			Assert.That(fileData, Text.Contains("<Name>Charlie</Name>"));
			Assert.That(fileData, Text.Contains("<ID>" + dog.ID + "</ID>"));

			/* sample...
				<?xml version="1.0" encoding="utf-8"?>
				<Pet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
				  <Name>Charlie</Name>
				  <ID>50ede263-62fe-406f-8973-212a338c72ad</ID>
				</Pet>
			*/
		}

		[Test]
		public void Create_a_default_Id()
		{
			string id = _db.CreateID();
			//Console.WriteLine(id);
			Assert.That(id, Is.Not.Null);
		}

		[Test]
		[ExpectedException(
			typeof(FileSystemDatabaseWriteException),
			ExpectedMessage = @"The character '/' is not a valid object identifier.")]
		public void Trying_to_save_an_item_with_a_backslash_throws_an_error()
		{
			Pet dog = new Pet { ID = "oi/etc", Name = "Charlie 2" };
			_db.Write(dog, dog.ID);
		}
	}
}